package com.link.base.base.report.service;

import com.link.base.base.coupons.model.CollectRecord;
import com.link.base.base.order.model.SaleOrder;
import com.link.base.base.report.dao.mybatis.mapper.AccntReportMapper;
import com.link.base.base.report.model.*;
import com.link.base.base.report.model.*;
import com.link.base.base.saleactivity.model.AccntCampMonitor;
import com.link.base.base.saleactivity.model.RaffleDetail;
import com.link.base.base.saleactivity.model.SaleActivity;
import com.link.base.base.salestarget.model.SalesTarget;
import com.link.base.base.wechat.model.PageData;
import com.link.core.basic.query.ExportColumn;
import com.link.core.basic.query.QueryParams;
import com.link.core.modules.export.ExportFactory;
import com.link.core.modules.export.ExportWriter;
import com.link.core.util.DateUtil;
import com.link.core.util.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author unknown
 * @date unknown
 * @comments 消费者报表
 */
@Service
public class AccntReportServiceImpl implements AccntReportService {
    @Resource
    private AccntReportMapper accntReportMapper;

    @Override
    public List<PortalBaseDataReport> queryAccountEverydayCount(PortalBaseDataReport account) throws Exception {

        return accntReportMapper.queryAccountEverydayCount(account);
    }

    @Override
    public List<String> getTimeList(String startTime, String endTime) throws ParseException {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date sdate = sdf.parse(startTime);
        List<String> result = new ArrayList<String>();
        result.add(startTime);
        result.add(endTime);
        Calendar tempStart = Calendar.getInstance();
        tempStart.setTime(sdate);
        tempStart.add(Calendar.DAY_OF_YEAR, 1);
        Calendar tempEnd = Calendar.getInstance();
        Date edate = sdf.parse(endTime);
        tempEnd.setTime(edate);
        while (tempStart.before(tempEnd)) {
            result.add(sdf.format(tempStart.getTime()));
            tempStart.add(Calendar.DAY_OF_YEAR, 1);
        }
        return result;
    }

    @Override
    public List<SalesTarget> querySalesTarget(SalesTarget salesTarget) {

        return accntReportMapper.querySalesTarget(salesTarget);
    }

    @Override
    public List<SaleOrder> querySaleOrders(SalesTarget salesTarget) {
        return accntReportMapper.querySaleOrders(salesTarget);
    }

    @Override
    public List<String> getMonthBetween(String startTime, String endTime) throws ParseException {
        ArrayList<String> result = new ArrayList<String>();
        //格式化为年月
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");

        Calendar min = Calendar.getInstance();
        Calendar max = Calendar.getInstance();

        min.setTime(sdf.parse(startTime));
        min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1);

        max.setTime(sdf.parse(endTime));
        max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2);

        Calendar curr = min;
        while (curr.before(max)) {
            result.add(sdf.format(curr.getTime()));
            curr.add(Calendar.MONTH, 1);
        }

        return result;
    }

    @Override
    public List<SaleOrder> queryorderTotalAmountForDay(SaleOrder order) {
        return accntReportMapper.queryorderTotalAmountForDay(order);
    }

    @Override
    public List<SaleOrder> querySaleOrderPro(SaleOrder order) {
        return accntReportMapper.querySaleOrderPro(order);
    }

    @Override
    public List<StockChangeReport> queryProdStockChangeReport(SaleOrder order) {
        return accntReportMapper.queryProdStockChangeReport(order);
    }

    @Override
    public List<SaleOrder> querySaleOrderProduct(SaleOrder order) {
        return accntReportMapper.querySaleOrderProduct(order);
    }

    @Override
    public List<RaffleDetail> activetyReport(RaffleDetail detail) {
        return accntReportMapper.activetyReport(detail);
    }

    @Override
    public List<CollectRecord> queryCollectRecord(CollectRecord collectRecord) {
        return accntReportMapper.queryCollectRecord(collectRecord);
    }

    private List<RaffleDetail> queryCoupon(RaffleDetail detail) {

        return accntReportMapper.queryCoupon(detail);
    }

    @Override
    public List<RaffleDetail> queryCouponForDay(String startTime, String endTime, String number, Long activityId,
                                                List<String> result) {
        RaffleDetail detail = new RaffleDetail();
        detail.setAttr1(startTime);
        detail.setAttr2(endTime);
        if ("01".equals(number)) {
            detail.setCouponsClass("Coupon");
        } else if ("03".equals(number)) {
            detail.setCouponsClass("PhysicalGift");
        }
        detail.setActivityId(activityId);
        detail.setWinOrnot("Y");
        List<RaffleDetail> details = queryCoupon(detail);
        Map<String, Object> map2 = new HashMap<>();
        //对查询数据按每天或者没有的时间顺序进行排列，如果某一天或者某一月没有数据设置为0
        String coupon = "Coupon";
        if (coupon.equals(detail.getCouponsClass())) {
            for (int i = 0; i < details.size(); i++) {
                String key = details.get(i).getAttr4();
                map2.put(key, details.get(i).getAttr5());
            }
            for (int i = 0; i < result.size(); i++) {
                if (null == map2.get(result.get(i))) {
                    RaffleDetail raffleDetail = new RaffleDetail();
                    raffleDetail.setAttr5("0");
                    raffleDetail.setAttr4(result.get(i));
                    details.add(raffleDetail);
                }
                // 排序
                Collections.sort(details, new Comparator() {
                    @Override
                    public int compare(Object o1, Object o2) {
                        RaffleDetail stu1 = (RaffleDetail) o1;
                        RaffleDetail stu2 = (RaffleDetail) o2;
                        return stu1.getAttr4().compareTo(stu2.getAttr4());
                    }
                });

            }
        } else {
            //对查询数据按每天或者没有的时间顺序进行排列，如果某一天或者某一月没有数据设置为0
            for (int i = 0; i < details.size(); i++) {
                String key = details.get(i).getAttr4();
                map2.put(key, details.get(i).getAttr5());
            }
            for (int i = 0; i < result.size(); i++) {
                if (null == map2.get(result.get(i))) {
                    RaffleDetail raffleDetail = new RaffleDetail();
                    raffleDetail.setAttr5("0");
                    raffleDetail.setAttr4(result.get(i));
                    details.add(raffleDetail);
                }
            }
            // 排序
            Collections.sort(details, new Comparator() {
                @Override
                public int compare(Object o1, Object o2) {
                    RaffleDetail stu1 = (RaffleDetail) o1;
                    RaffleDetail stu2 = (RaffleDetail) o2;
                    return stu1.getAttr4().compareTo(stu2.getAttr4());
                }
            });
        }
        return details;
    }

    @Override
    public List<RaffleDetail> queryCouponForDaybynum(String startTime, String endTime, String string, Long activityId,
                                                     List<String> result) {
        RaffleDetail detail = new RaffleDetail();
        detail.setAttr1(startTime);
        detail.setAttr2(endTime);
        if ("05".equals(string)) {
            detail.setCouponsClass("Coupon");
        } else if ("07".equals(string)) {
            detail.setCouponsClass("PhysicalGift");
        }
        detail.setActivityId(activityId);
        detail.setWinOrnot("Y");
        Map<String, Object> map2 = new HashMap<>();
        List<RaffleDetail> details = queryCoupontwo(detail);
        //对查询数据按每天或者没有的时间顺序进行排列，如果某一天或者某一月没有数据设置为0
        String coupon = "Coupon";
        if (coupon.equals(detail.getCouponsClass())) {
            for (int i = 0; i < details.size(); i++) {
                String key = details.get(i).getAttr4();
                map2.put(key, details.get(i).getAttr5());
            }
            for (int i = 0; i < result.size(); i++) {
                if (null == map2.get(result.get(i))) {
                    RaffleDetail raffleDetail = new RaffleDetail();
                    raffleDetail.setAttr5("0");
                    raffleDetail.setAttr4(result.get(i));
                    details.add(raffleDetail);
                }
                // 排序
                Collections.sort(details, new Comparator() {
                    @Override
                    public int compare(Object o1, Object o2) {
                        RaffleDetail stu1 = (RaffleDetail) o1;
                        RaffleDetail stu2 = (RaffleDetail) o2;
                        return stu1.getAttr4().compareTo(stu2.getAttr4());
                    }
                });

            }
        } else {
            //对查询数据按每天或者没有的时间顺序进行排列，如果某一天或者某一月没有数据设置为0
            for (int i = 0; i < details.size(); i++) {
                String key = details.get(i).getAttr4();
                map2.put(key, details.get(i).getAttr5());
            }
            for (int i = 0; i < result.size(); i++) {
                if (null == map2.get(result.get(i))) {
                    RaffleDetail raffleDetail = new RaffleDetail();
                    raffleDetail.setAttr5("0");
                    raffleDetail.setAttr4(result.get(i));
                    details.add(raffleDetail);
                }
            }
            // 排序
            Collections.sort(details, new Comparator() {
                @Override
                public int compare(Object o1, Object o2) {
                    RaffleDetail stu1 = (RaffleDetail) o1;
                    RaffleDetail stu2 = (RaffleDetail) o2;
                    return stu1.getAttr4().compareTo(stu2.getAttr4());
                }
            });
        }
        return details;
    }

    private List<RaffleDetail> queryCoupontwo(RaffleDetail detail) {
        return accntReportMapper.queryCoupontwo(detail);
    }

    @Override
    public List<CollectRecord> querycollectRecordsForDay(String startTime, String endTime, String string,
                                                         Long activityId, List<String> result) {
        Map<String, Object> map2 = new HashMap<>();
        CollectRecord collectRecord = new CollectRecord();
        collectRecord.setAttr1(startTime);
        collectRecord.setAttr2(endTime);
        if ("02".equals(string)) {
            collectRecord.setCouponsClass("Coupon");
        } else if ("04".equals(string)) {
            collectRecord.setCouponsClass("PhysicalGift");
        }
        collectRecord.setAttr5(activityId.toString());
        List<CollectRecord> details = querycollectRecordsForDay(collectRecord);
        String coupon = "Coupon";
        if (coupon.equals(collectRecord.getCouponsClass())) {
            for (int i = 0; i < details.size(); i++) {
                String key = details.get(i).getAttr4();
                map2.put(key, details.get(i).getAttr5());
            }
            for (int i = 0; i < result.size(); i++) {
                if (null == map2.get(result.get(i))) {
                    CollectRecord raffleDetail = new CollectRecord();
                    raffleDetail.setAttr5("0");
                    raffleDetail.setAttr4(result.get(i));
                    details.add(raffleDetail);
                }
                // 排序
                Collections.sort(details, new Comparator() {
                    @Override
                    public int compare(Object o1, Object o2) {
                        CollectRecord stu1 = (CollectRecord) o1;
                        CollectRecord stu2 = (CollectRecord) o2;
                        return stu1.getAttr4().compareTo(stu2.getAttr4());
                    }
                });

            }
        } else {
            for (int i = 0; i < details.size(); i++) {
                String key = details.get(i).getAttr4();
                map2.put(key, details.get(i).getAttr5());
            }
            for (int i = 0; i < result.size(); i++) {
                if (null == map2.get(result.get(i))) {
                    CollectRecord raffleDetail = new CollectRecord();
                    raffleDetail.setAttr5("0");
                    raffleDetail.setAttr4(result.get(i));
                    details.add(raffleDetail);
                }
            }
            // 排序
            Collections.sort(details, new Comparator() {
                @Override
                public int compare(Object o1, Object o2) {
                    CollectRecord stu1 = (CollectRecord) o1;
                    CollectRecord stu2 = (CollectRecord) o2;
                    return stu1.getAttr4().compareTo(stu2.getAttr4());
                }
            });
        }
        return details;
    }

    private List<CollectRecord> querycollectRecordsForDay(CollectRecord collectRecord) {
        return accntReportMapper.querycollectRecordsForDay(collectRecord);
    }

    @Override
    public List<CollectRecord> querycollectRecordsForDaynum(String startTime, String endTime, String string,
                                                            Long activityId, List<String> result) {
        Map<String, Object> map2 = new HashMap<>();
        CollectRecord collectRecord = new CollectRecord();
        collectRecord.setAttr1(startTime);
        collectRecord.setAttr2(endTime);
        if ("02".equals(string)) {
            collectRecord.setCouponsClass("Coupon");
        } else if ("04".equals(string)) {
            collectRecord.setCouponsClass("PhysicalGift");
        }
        collectRecord.setAttr5(activityId.toString());
        List<CollectRecord> details = querycollectRecordsForDaynum(collectRecord);
        String coupon = "Coupon";
        if (coupon.equals(collectRecord.getCouponsClass())) {
            for (int i = 0; i < details.size(); i++) {
                String key = details.get(i).getAttr4();
                map2.put(key, details.get(i).getAttr5());
            }
            for (int i = 0; i < result.size(); i++) {
                if (null == map2.get(result.get(i))) {
                    CollectRecord raffleDetail = new CollectRecord();
                    raffleDetail.setAttr5("0");
                    raffleDetail.setAttr4(result.get(i));
                    details.add(raffleDetail);
                }
                // 排序
                Collections.sort(details, new Comparator() {
                    @Override
                    public int compare(Object o1, Object o2) {
                        CollectRecord stu1 = (CollectRecord) o1;
                        CollectRecord stu2 = (CollectRecord) o2;
                        return stu1.getAttr4().compareTo(stu2.getAttr4());
                    }
                });

            }
        } else {
            for (int i = 0; i < details.size(); i++) {
                String key = details.get(i).getAttr4();
                map2.put(key, details.get(i).getAttr5());
            }
            for (int i = 0; i < result.size(); i++) {
                if (null == map2.get(result.get(i))) {
                    CollectRecord raffleDetail = new CollectRecord();
                    raffleDetail.setAttr5("0");
                    raffleDetail.setAttr4(result.get(i));
                    details.add(raffleDetail);
                }
            }
            // 排序
            Collections.sort(details, new Comparator() {
                @Override
                public int compare(Object o1, Object o2) {
                    CollectRecord stu1 = (CollectRecord) o1;
                    CollectRecord stu2 = (CollectRecord) o2;
                    return stu1.getAttr4().compareTo(stu2.getAttr4());
                }
            });
        }
        return details;
    }

    private List<CollectRecord> querycollectRecordsForDaynum(CollectRecord collectRecord) {
        return accntReportMapper.querycollectRecordsForDaynum(collectRecord);
    }

    @Override
    public SaleOrder querySaleOrderSum(SaleOrder order) {
        return accntReportMapper.querySaleOrderSum(order);
    }

    @Override
    public Object queryallCouponForDay(String startTime, String endTime, String string, Long activityId,
                                       List<String> result) {
        RaffleDetail detail = new RaffleDetail();
        detail.setAttr1(startTime);
        detail.setAttr2(endTime);
        detail.setActivityId(activityId);
        List<RaffleDetail> details = queryCoupon(detail);
        Map<String, Object> map2 = new HashMap<>();
        //对查询数据按每天或者没有的时间顺序进行排列，如果某一天或者某一月没有数据设置为0
        for (int i = 0; i < details.size(); i++) {
            String key = details.get(i).getAttr4();
            map2.put(key, details.get(i).getAttr5());
        }
        for (int i = 0; i < result.size(); i++) {
            if (null == map2.get(result.get(i))) {
                RaffleDetail raffleDetail = new RaffleDetail();
                raffleDetail.setAttr5("0");
                raffleDetail.setAttr4(result.get(i));
                details.add(raffleDetail);
            }
            // 排序
            Collections.sort(details, new Comparator() {
                @Override
                public int compare(Object o1, Object o2) {
                    RaffleDetail stu1 = (RaffleDetail) o1;
                    RaffleDetail stu2 = (RaffleDetail) o2;
                    return stu1.getAttr4().compareTo(stu2.getAttr4());
                }
            });

        }
        return details;
    }

    @Override
    public Object queryallCouponnum(String startTime, String endTime, String string, Long activityId,
                                    List<String> result) {
        RaffleDetail detail = new RaffleDetail();
        detail.setAttr1(startTime);
        detail.setAttr2(endTime);
        detail.setActivityId(activityId);
        Map<String, Object> map2 = new HashMap<>();
        List<RaffleDetail> details = queryCoupontwo(detail);
        //对查询数据按每天或者没有的时间顺序进行排列，如果某一天或者某一月没有数据设置为0
        for (int i = 0; i < details.size(); i++) {
            String key = details.get(i).getAttr4();
            map2.put(key, details.get(i).getAttr5());
        }
        for (int i = 0; i < result.size(); i++) {
            if (null == map2.get(result.get(i))) {
                RaffleDetail raffleDetail = new RaffleDetail();
                raffleDetail.setAttr5("0");
                raffleDetail.setAttr4(result.get(i));
                details.add(raffleDetail);
            }
            // 排序
            Collections.sort(details, new Comparator() {
                @Override
                public int compare(Object o1, Object o2) {
                    RaffleDetail stu1 = (RaffleDetail) o1;
                    RaffleDetail stu2 = (RaffleDetail) o2;
                    return stu1.getAttr4().compareTo(stu2.getAttr4());
                }
            });

        }

        return details;
    }

    @Override
    public SaleActivity activitycount(SaleActivity activity) {
        return accntReportMapper.activitycount(activity);
    }

    @Override
    public List<CollectRecord> queryCouponscount(CollectRecord collectRecord) {
        return accntReportMapper.queryCouponscount(collectRecord);
    }

    @Override
    public List<CollectRecord> queryCouponsUsecount(CollectRecord collectRecord) {
        return accntReportMapper.queryCouponsUsecount(collectRecord);
    }

    @Override
    public List<SaleActivity> NumberOfActivities(String startTime, String endTime, String string, List<String> result) {
        return null;
    }

    @Override
    public List<CollectRecord> CouponsNumberOfPeople(String startTime, String endTime, String string,
                                                     List<String> result, String type, String id) {
        CollectRecord collectRecord = new CollectRecord();
        collectRecord.setAttr1(startTime);
        collectRecord.setAttr2(endTime);
        collectRecord.setAttr3(type);
        collectRecord.setAttr7(id);
        List<CollectRecord> collectRecords = new ArrayList<>();
        Map<String, Object> map2 = new HashMap<>();
        String use = "use";
        String claim = "claim";
        if (use.equals(type)) {
            collectRecords = accntReportMapper.couponsNumberOfPeople1(collectRecord);
        } else if (claim.equals(type)) {
            collectRecords = accntReportMapper.couponsNumberOfPeople(collectRecord);
        }
        //对查询数据按每天或者没有的时间顺序进行排列，如果某一天或者某一月没有数据设置为0
        for (int i = 0; i < collectRecords.size(); i++) {
            String key = collectRecords.get(i).getAttr4();
            map2.put(key, collectRecords.get(i).getAttr5());
        }
        for (int i = 0; i < result.size(); i++) {
            if (null == map2.get(result.get(i))) {
                CollectRecord raffleDetail = new CollectRecord();
                raffleDetail.setAttr5("0");
                raffleDetail.setAttr4(result.get(i));
                collectRecords.add(raffleDetail);
            }
            // 排序
            Collections.sort(collectRecords, new Comparator() {
                @Override
                public int compare(Object o1, Object o2) {
                    CollectRecord stu1 = (CollectRecord) o1;
                    CollectRecord stu2 = (CollectRecord) o2;
                    return stu1.getAttr4().compareTo(stu2.getAttr4());
                }
            });

        }

        return collectRecords;
    }

    @Override
    public List<CollectRecord> CouponsNumber(String startTime, String endTime, String string, List<String> result, String type, String id) {
        CollectRecord collectRecord = new CollectRecord();
        collectRecord.setAttr1(startTime);
        collectRecord.setAttr2(endTime);
        collectRecord.setAttr3(type);
        collectRecord.setAttr7(id);
        List<CollectRecord> collectRecords = new ArrayList<>();
        Map<String, Object> map2 = new HashMap<>();
        String use = "use";
        String claim = "claim";
        if (use.equals(type)) {
            collectRecords = accntReportMapper.couponsNumberOfNumber1(collectRecord);
        } else if (claim.equals(type)) {
            collectRecords = accntReportMapper.couponsNumberOfNumber(collectRecord);
        }
        //对查询数据按每天或者没有的时间顺序进行排列，如果某一天或者某一月没有数据设置为0
        for (int i = 0; i < collectRecords.size(); i++) {
            String key = collectRecords.get(i).getAttr4();
            map2.put(key, collectRecords.get(i).getAttr5());
        }
        for (int i = 0; i < result.size(); i++) {
            if (null == map2.get(result.get(i))) {
                CollectRecord raffleDetail = new CollectRecord();
                raffleDetail.setAttr5("0");
                raffleDetail.setAttr4(result.get(i));
                collectRecords.add(raffleDetail);
            }
            // 排序
            Collections.sort(collectRecords, new Comparator() {
                @Override
                public int compare(Object o1, Object o2) {
                    CollectRecord stu1 = (CollectRecord) o1;
                    CollectRecord stu2 = (CollectRecord) o2;
                    return stu1.getAttr4().compareTo(stu2.getAttr4());
                }
            });

        }

        return collectRecords;
    }

    @SuppressWarnings("unchecked")
    @Override
    public Map<String, Object> campaignResultExport(Map<String, Object> map, QueryParams qps, String type) throws Exception {

        //获取数据
        List<PageData> list = new ArrayList<>();
        List<SaleActivity> details = (List<SaleActivity>) map.get("a");
        List<CollectRecord> collectRecords = (List<CollectRecord>) map.get("b");
        List<CollectRecord> details1 = (List<CollectRecord>) map.get("c");
        List<CollectRecord> collectRecords1 = (List<CollectRecord>) map.get("d");
        List<CollectRecord> details11 = (List<CollectRecord>) map.get("e");

        int size = Integer.parseInt(map.get("size").toString());
        for (int i = 0; i < size; i++) {
            PageData data = new PageData();
            list.add(data);
        }
        for (int i = 0; i < size; i++) {
            if (null != details) {
                list.get(i).setAttr1(details.get(i).getAttr5());
                list.get(i).setDate(details.get(i).getAttr4());
            }
            if (null != collectRecords) {
                list.get(i).setAttr2(collectRecords.get(i).getAttr5());
                list.get(i).setDate(collectRecords.get(i).getAttr4());
            }
            if (null != details1) {
                list.get(i).setAttr3(details1.get(i).getAttr5());
                list.get(i).setDate(details1.get(i).getAttr4());
            }
            if (null != collectRecords1) {
                list.get(i).setAttr4(collectRecords1.get(i).getAttr5());
                list.get(i).setDate(collectRecords1.get(i).getAttr4());
            }
            if (null != details11) {
                list.get(i).setAttr5(details11.get(i).getAttr5());
                list.get(i).setDate(details11.get(i).getAttr4());
            }

        }
        qps.setTotal(size);
        ExportColumn column = new ExportColumn();
        ExportColumn column1 = new ExportColumn();
        ExportColumn column2 = new ExportColumn();

        ExportColumn column8 = new ExportColumn();
        column8.setField("date");

        column1.setField("attr2");
        column2.setField("attr3");
        ExportColumn column3 = new ExportColumn();
        column3.setField("attr4");
        ExportColumn column4 = new ExportColumn();
        column4.setField("attr5");
        if (!"01".equals(type)) {
            column.setField("attr1");
            column.setTitle("活动数量");
            column.setOrder(1);
        }

        column1.setTitle("优惠券领取人数");
        column2.setTitle("优惠券领取次数");
        column3.setTitle("优惠券使用人数");
        column4.setTitle("优惠券使用次数");

        column8.setTitle("时间");

        column1.setOrder(2);
        column2.setOrder(3);
        column3.setOrder(4);
        column4.setOrder(5);

        column8.setOrder(0);

        List<ExportColumn> exportColumns = new ArrayList<>();
        exportColumns.add(column4);
        exportColumns.add(column3);
        exportColumns.add(column2);
        exportColumns.add(column1);
        exportColumns.add(column);
        exportColumns.add(column8);
        ExportWriter exportWriter = ExportFactory.newExcelInstance(qps);
        qps.setExportColumns(exportColumns);
        exportWriter.wirteData(list);
        Map<String, Object> result = new HashMap<>();
        result.put("url", exportWriter.getUrlPath());
        return result;
    }

    @SuppressWarnings("unchecked")
    @Override
    public Map<String, Object> campaignActivityResultExport(Map<String, Object> map, QueryParams qps) throws Exception {

        //获取数据
        List<PageData> list = new ArrayList<>();
        List<SaleActivity> details = (List<SaleActivity>) map.get("a");
        List<CollectRecord> collectRecords = (List<CollectRecord>) map.get("b");
        List<CollectRecord> details1 = (List<CollectRecord>) map.get("c");
        List<CollectRecord> collectRecords1 = (List<CollectRecord>) map.get("d");
        List<CollectRecord> details11 = (List<CollectRecord>) map.get("e");

        int size = Integer.parseInt(map.get("size").toString());
        for (int i = 0; i < size; i++) {
            PageData data = new PageData();
            list.add(data);
        }
        for (int i = 0; i < size; i++) {
            if (null != details) {
                list.get(i).setAttr1(details.get(i).getAttr5());
                list.get(i).setDate(details.get(i).getAttr4());
            }
            if (null != collectRecords) {
                list.get(i).setAttr2(collectRecords.get(i).getAttr5());
                list.get(i).setDate(collectRecords.get(i).getAttr4());
            }
            if (null != details1) {
                list.get(i).setAttr3(details1.get(i).getAttr5());
                list.get(i).setDate(details1.get(i).getAttr4());
            }
            if (null != collectRecords1) {
                list.get(i).setAttr4(collectRecords1.get(i).getAttr5());
                list.get(i).setDate(collectRecords1.get(i).getAttr4());
            }
            if (null != details11) {
                list.get(i).setAttr5(details11.get(i).getAttr5());
                list.get(i).setDate(details11.get(i).getAttr4());
            }

        }
        qps.setTotal(size);
        //ExportColumn column = new ExportColumn();
        ExportColumn column1 = new ExportColumn();
        ExportColumn column2 = new ExportColumn();

        ExportColumn column8 = new ExportColumn();
        column8.setField("date");

        column1.setField("attr1");
        column2.setField("attr2");
        ExportColumn column3 = new ExportColumn();
        column3.setField("attr3");
        ExportColumn column4 = new ExportColumn();
        column4.setField("attr4");


        column1.setTitle("优惠券领取人数");
        column2.setTitle("优惠券领取次数");
        column3.setTitle("优惠券使用人数");
        column4.setTitle("优惠券使用次数");

        column8.setTitle("时间");

        column1.setOrder(1);
        column2.setOrder(2);
        column3.setOrder(3);
        column4.setOrder(4);

        column8.setOrder(0);
        List<ExportColumn> exportColumns = new ArrayList<>();

        exportColumns.add(column4);
        exportColumns.add(column3);
        exportColumns.add(column2);
        exportColumns.add(column1);
        exportColumns.add(column8);
        ExportWriter exportWriter = ExportFactory.newExcelInstance(qps);
        qps.setExportColumns(exportColumns);
        exportWriter.wirteData(list);
        Map<String, Object> result = new HashMap<>();
        result.put("url", exportWriter.getUrlPath());
        return result;
    }

    @Override
    public List<SalesTarget> querySalesTargetByShopper(SalesTarget salesTarget) throws Exception {
        return accntReportMapper.querySalesTargetByShopper(salesTarget);
    }

    @Override
    public List<SaleOrder> querySaleOrdersByShopper(SalesTarget salesTarget) throws Exception {
        return accntReportMapper.querySaleOrdersByShopper(salesTarget);
    }

    @Override
    public GoalAchievement queryGoalAchievement(GoalAchievement goalAchievement) throws Exception {
        GoalAchievement result = new GoalAchievement();
        Double goal = accntReportMapper.queryGoal(goalAchievement);
        Double achieve = accntReportMapper.queryAchieve(goalAchievement);
        String zeroValue = "0.00";
        // 保留两位小数
        String pattern = "0.00";
        DecimalFormat df = new DecimalFormat(pattern);
        if (ObjectUtils.isEmpty(goal)) {
            result.setTaskVolumeMonth(zeroValue);
            result.setAchieveAmtMonth(df.format(achieve));
            result.setAchieveRadio(zeroValue);
        } else {
            double achieveRadio = achieve / goal * 100;
            result.setTaskVolumeMonth(df.format(goal));
            result.setAchieveAmtMonth(df.format(achieve));
            result.setAchieveRadio(df.format(achieveRadio));
        }
        return result;
    }

    @Override
    public AcctAnys queryAcctAnysReport(AcctAnys acctAnys) throws Exception {
        List<AcctAnys> datalist = accntReportMapper.queryAcctAnysReport(acctAnys);
        BigDecimal totalAcctNum = BigDecimal.valueOf(0);
        BigDecimal totalOrderAcctNum = BigDecimal.valueOf(0);
        BigDecimal totalTodaySaleNum = BigDecimal.valueOf(0);
        BigDecimal totalYesterdaySaleNum = BigDecimal.valueOf(0);
        BigDecimal totalRatherYesterdayNum = BigDecimal.valueOf(0);
        if (datalist != null && datalist.size() > 0) {
            // 遍历各省份销售结果集，取当日销售和、昨日销售和
            for (AcctAnys entity : datalist) {
                if (entity == null) {
                    totalAcctNum = totalAcctNum.add(BigDecimal.valueOf(0));
                    totalOrderAcctNum = totalOrderAcctNum.add(BigDecimal.valueOf(0));
                    totalTodaySaleNum = totalTodaySaleNum.add(BigDecimal.valueOf(0));
                    totalYesterdaySaleNum = totalYesterdaySaleNum.add(BigDecimal.valueOf(0));
                } else {
                    totalAcctNum = totalAcctNum.add(StringUtils.isBlank(entity.getAccountNum())
                            ? BigDecimal.valueOf(0) : new BigDecimal(entity.getAccountNum()));
                    totalOrderAcctNum = totalOrderAcctNum.add(StringUtils.isBlank(entity.getOrderAcctNum())
                            ? BigDecimal.valueOf(0) : new BigDecimal(entity.getOrderAcctNum()));
                    BigDecimal todaySaleNum = StringUtils.isBlank(entity.getValue())
                            ? BigDecimal.valueOf(0) : new BigDecimal(entity.getValue());
                    totalTodaySaleNum = totalTodaySaleNum.add(todaySaleNum);
                    BigDecimal yesterdaySaleNum = StringUtils.isBlank(entity.getYesterdaySaleNum())
                            ? BigDecimal.valueOf(0) : new BigDecimal(entity.getYesterdaySaleNum());
                    totalYesterdaySaleNum = totalYesterdaySaleNum.add(yesterdaySaleNum);
                    totalRatherYesterdayNum = totalRatherYesterdayNum.add(todaySaleNum).subtract(yesterdaySaleNum);
                    BigDecimal ratherYesterdayNum = todaySaleNum.subtract(yesterdaySaleNum);
                    if (ratherYesterdayNum.compareTo(BigDecimal.valueOf(0)) == 0) {
                        entity.setRatherYesterdayNum("0.00");
                    } else {
                        entity.setRatherYesterdayNum(ratherYesterdayNum.toString());
                    }
                }
            }
        }
        AcctAnys result = new AcctAnys();
        totalAcctNum = totalAcctNum.compareTo(BigDecimal.valueOf(0)) == 0 ? BigDecimal.valueOf(0.00) : totalAcctNum;
        result.setAccountNum(totalAcctNum.toString());
        totalOrderAcctNum = totalOrderAcctNum.compareTo(BigDecimal.valueOf(0)) == 0 ? BigDecimal.valueOf(0.00) : totalOrderAcctNum;
        result.setOrderAcctNum(totalOrderAcctNum.toString());
        totalTodaySaleNum = totalTodaySaleNum.compareTo(BigDecimal.valueOf(0)) == 0 ? BigDecimal.valueOf(0.00) : totalTodaySaleNum;
        result.setValue(totalTodaySaleNum.toString());
        totalYesterdaySaleNum = totalYesterdaySaleNum.compareTo(BigDecimal.valueOf(0)) == 0 ? BigDecimal.valueOf(0.00) : totalYesterdaySaleNum;
        result.setYesterdaySaleNum(totalYesterdaySaleNum.toString());
        totalRatherYesterdayNum = totalRatherYesterdayNum.compareTo(BigDecimal.valueOf(0)) == 0 ? BigDecimal.valueOf(0.00) : totalRatherYesterdayNum;
        result.setRatherYesterdayNum(totalRatherYesterdayNum.toString());
        result.setDataList(datalist);
        return result;
    }

    @Override
    public List<AcctAnys> queryByAcctAnysNoMonthDetail(AcctAnys acctAnys) throws Exception {
        List<AcctAnys> dataList = accntReportMapper.queryByAcctAnysNoMonthDetail(acctAnys);
        List<AcctAnys> resultList = new ArrayList<>();
        if (dataList != null && dataList.size() > 0) {
            // 筛选掉当日销售不为0的数据
            for (AcctAnys entity : dataList) {
                BigDecimal todaySaleNum = StringUtils.isBlank(entity.getValue()) ? BigDecimal.valueOf(0) : new BigDecimal(entity.getValue());
                if (todaySaleNum.compareTo(BigDecimal.valueOf(0)) != 0) {
                    resultList.add(entity);
                }
            }
        }
        return resultList;
    }

    @Override
    public AcctAnys queryMonthTargetSale(AcctAnys acctAnys) throws Exception {
        List<AcctAnys> datalist = accntReportMapper.queryMonthTargetSale(acctAnys);
        BigDecimal totalTargetNum = BigDecimal.valueOf(0);
        BigDecimal totalSaleNum = BigDecimal.valueOf(0);
        BigDecimal totalReach = BigDecimal.valueOf(0);
        // 计算数据的目标达成比及汇总数据
        if (datalist != null && datalist.size() > 0) {
            for (AcctAnys entity : datalist) {
                if (entity == null) {
                    totalTargetNum = totalTargetNum.add(BigDecimal.valueOf(0));
                    totalSaleNum = totalSaleNum.add(BigDecimal.valueOf(0));
                } else {
                    BigDecimal targetNum = StringUtils.isBlank(entity.getTargetNum()) ? BigDecimal.valueOf(0) : new BigDecimal(entity.getTargetNum());
                    BigDecimal saleNum = StringUtils.isBlank(entity.getSaleNum()) ? BigDecimal.valueOf(0) : new BigDecimal(entity.getSaleNum());
                    totalTargetNum = totalTargetNum.add(targetNum);
                    totalSaleNum = totalSaleNum.add(saleNum);
                    if (targetNum.compareTo(BigDecimal.valueOf(0)) == 0) {
                        entity.setValue("0");
                    } else {
                        BigDecimal reachNum = saleNum.divide(targetNum, 2, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal("100"));
                        entity.setValue(reachNum.toString());
                    }
                }
            }
        }
        AcctAnys result = new AcctAnys();
        result.setTargetNum(totalTargetNum.toString());
        result.setSaleNum(totalSaleNum.toString());
        if (totalTargetNum.compareTo(BigDecimal.valueOf(0)) != 0) {
            totalReach = totalSaleNum.divide(totalTargetNum, 2, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal("100"));
        }
        result.setValue(totalReach.toString());
        result.setDataList(datalist);
        return result;
    }

    @Override
    public List<AcctAnys> queryMonthTargetSaleDetail(AcctAnys acctAnys) throws Exception {
        List<AcctAnys> datalist = accntReportMapper.queryMonthTargetSaleDetail(acctAnys);
        if (datalist != null && datalist.size() > 0) {
            for (AcctAnys entity : datalist) {
                if (entity != null) {
                    BigDecimal targetNum = StringUtils.isBlank(entity.getTargetNum()) ? BigDecimal.valueOf(0) : new BigDecimal(entity.getTargetNum());
                    BigDecimal saleNum = StringUtils.isBlank(entity.getSaleNum()) ? BigDecimal.valueOf(0) : new BigDecimal(entity.getSaleNum());
                    if (targetNum.compareTo(BigDecimal.valueOf(0)) == 0) {
                        entity.setValue("0");
                    } else {
                        BigDecimal reachNum = saleNum.divide(targetNum, 2, BigDecimal.ROUND_HALF_UP);
                        entity.setValue(reachNum.toString());
                    }
                }
            }
        }
        return datalist;
    }

    @Override
    public List<AcctCampReport> queryAcctCampByDateRange(String startTime, String endTime) throws Exception {
        List<AcctCampReport> res = new ArrayList<>();
        AccntCampMonitor param = new AccntCampMonitor();
        param.setStartTime(startTime);
        param.setEndTime(endTime);
        List<AcctCampReport> list = accntReportMapper.queryAccntCount(param);
        Collections.sort(list, new Comparator<AcctCampReport>() {
            @Override
            public int compare(AcctCampReport o1, AcctCampReport o2) {
                if (o1.getNum() < o2.getNum()) {
                    return 1;
                }
                if (o1.getNum() == o2.getNum()) {
                    return 0;
                }
                return -1;
            }
        });
        if (list != null && list.size() > 10) {
            res = list.subList(0, 10);
        } else {
            res = list;
        }

        return res;
    }

    @Override
    public List<AcctCampReport> queryAcctCamp(String latelyNum) throws Exception {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        //本周消费者推荐排行
        if (StringUtils.equals("7", latelyNum)) {
            //获取本周开始时间
            Calendar cal = Calendar.getInstance();
            cal.set(Calendar.DAY_OF_WEEK, cal.getFirstDayOfWeek());
            cal.add(Calendar.DAY_OF_WEEK,1);
            String startDate = simpleDateFormat.format(cal.getTime());
            //获取本周结束时间
            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.DAY_OF_WEEK, calendar.getFirstDayOfWeek());
            calendar.add(Calendar.DAY_OF_WEEK,1);
            calendar.add(Calendar.DATE,7);
            String endDate = simpleDateFormat.format(calendar.getTime());

            return queryAcctCampByDateRange(startDate, endDate);
        }
        //本月消费者推荐排行
        if (StringUtils.equals("30", latelyNum)) {
            //获得本月起始时间
            Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.MONTH, 0);
            calendar.set(Calendar.DAY_OF_MONTH, 1);
            String startDate = simpleDateFormat.format(calendar.getTime());
            //获得本月结束时间
            Calendar cal = Calendar.getInstance();
            cal.set(Calendar.DAY_OF_MONTH,1);
            cal.add(Calendar.MONTH, 1);
            String endDate = simpleDateFormat.format(cal.getTime());

            return queryAcctCampByDateRange(startDate, endDate);
        }
        //本年消费者推荐排行
        if (StringUtils.equals("365", latelyNum)) {
            String startDate = DateUtil.getCurrentYear() + "-01-01";
            String endDate = DateUtil.getCurrentYear() + "-12-31 23:59:59";

            return queryAcctCampByDateRange(startDate, endDate);
        }
        return null;
    }

}
